/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.utils;

import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.modules.sys.entity.Menu;
import com.vidmt.api.common.anno.WebTldUse;
import com.vidmt.api.common.cache.MyCacheManager;
import com.vidmt.api.modules.sys.Acc;
import com.vidmt.api.modules.sys.Enums.AccType;
import com.vidmt.api.modules.sys.entity.User;
import com.vidmt.api.modules.sys.service.MenuService;
import com.vidmt.api.modules.sys.service.UserService;
import com.vidmt.api.modules.sys.shiro.MyPrincipal;

/**
 * 用户工具类
 * 
 * @author ThinkGem
 * @version 2013-12-05
 */
public class UserUtils {
	private static MenuService menuService = SpringContextHolder.getBean(MenuService.class);
	private static UserService userService = SpringContextHolder.getBean(UserService.class);
	private static MyCacheManager myCacheManager = SpringContextHolder.getBean(MyCacheManager.class);

	public static final String CACHE_MENU_LIST = "menuList";
	public static final String CACHE_AREA_LIST = "areaList";

	@WebTldUse
	public static String getUserName() {
		MyPrincipal principal = (MyPrincipal) SecurityUtils.getSubject().getPrincipal();
		return principal.getDisplayName();
	}

	public static String findPwdByAcc(Acc acc) {
		Long uid = null;
		if (acc.type() == AccType.uid) {
			uid = Long.valueOf(acc.value());
		} else {
			User user = userService.findByAcc(acc);
			uid = user.getId();
		}

		return userService.getPwd(uid);
	}

	@WebTldUse
	public static User get(Long id) {
		return userService.load(id);
	}

	/**
	 * 获取amdin用户授权菜单
	 * 
	 * @return
	 */
	@WebTldUse
	public static List<Menu> getAllMenuList() {
		@SuppressWarnings("unchecked")
		List<Menu> menuList = (List<Menu>) getSessionKey(CACHE_MENU_LIST);
		if (menuList == null) {
			menuList = menuService.findMenuList();
			putSessionKey(CACHE_MENU_LIST, menuList);
		}
		return menuList;
	}


	@WebTldUse
	public static MyPrincipal getPrincipal() {
		Subject sbj = SecurityUtils.getSubject();
		return (MyPrincipal) sbj.getPrincipal();
	}

	/**
	 * 获取授权主要对象
	 */
	public static Subject getSubject() {
		return SecurityUtils.getSubject();
	}

	public static User getCurrentUser() {
		MyPrincipal principal = (MyPrincipal) SecurityUtils.getSubject().getPrincipal();
		if (principal != null) {
			return principal.getUser();
		}
		return null;
	}

	public static Session getSession() {
		try {
			Subject subject = SecurityUtils.getSubject();
			Session session = subject.getSession(false);
			if (session == null) {
				session = subject.getSession();
			}
			if (session != null) {
				return session;
			}
			// subject.logout();
		} catch (InvalidSessionException e) {

		}
		return null;
	}

	public static boolean isValidateCodeLogin(String username, boolean isFail, boolean clean) {
		@SuppressWarnings("unchecked")
		Map<String, Integer> loginFailMap = (Map<String, Integer>) myCacheManager.get(MyCacheManager.SYS_CACHE,
				"loginFailMap");
		if (loginFailMap == null) {
			loginFailMap = Maps.newHashMap();
			myCacheManager.put(MyCacheManager.SYS_CACHE, "loginFailMap", loginFailMap);
		}
		Integer loginFailNum = loginFailMap.get(username);
		if (loginFailNum == null) {
			loginFailNum = 0;
		}
		if (isFail) {
			loginFailNum++;
			loginFailMap.put(username, loginFailNum);
		}
		if (clean) {
			loginFailMap.remove(username);
		}
		return loginFailNum >= 3;
	}
	// ============== User Cache ==============

	public static Object getSessionKey(String key) {
		// Object obj = getCacheMap().get(key);
		return getSession().getAttribute(key);
	}

	public static void putSessionKey(String key, Object value) {
		// getCacheMap().put(key, value);
		getSession().setAttribute(key, value);
	}
}
